const router = require('koa-router')()
const basiAuth = require("basic-auth")
// 微信云开发token
const {
    getToken,
    selurl,
    updurl
} = require('../../config/databaseapi')
const {
    showtoken
} = require("../../token/showtoken")
// 操作数据库
const result = require('../../config/result')
// 校验
const {
    regcheck,
    shoinfo
} = require("../../config/register")
// 图片上传
// after 后台服务器
router.prefix('/api/after')



// 获取分类内容 查询
router.get('/obtainunit', new showtoken().m, async ctx => {
    const query = `db.collection('dishunit').get()`
    try {
        const res = await new getToken().postlive(selurl, query)
        const data = res.data.map(item => {
            return JSON.parse(item)
        })
        new result(ctx, 'SUCCESS', 200, data).answer()
    } catch (error) {
        new result(ctx, "服务器出现错误", 500).answer()
        return error
    }
})

// 查询口味分类 口味 查询
router.get('/capacity', new showtoken().m, async ctx => {
    const query = `db.collection('capacity').get()`
    try {
        const res = await new getToken().postlive(selurl, query)
        const data = res.data.map(item => {
            return JSON.parse(item)
        })
        new result(ctx, 'SUCCESS', 200, data).answer()
    } catch (error) {
        new result(ctx, "服务器出现错误", 500).answer()
        return error
    }
})
// 查询口味分类 容量 查询
router.get('/flavor', new showtoken().m, async ctx => {
    const query = `db.collection('flavor').get()`
    try {
        const res = await new getToken().postlive(selurl, query)
        const data = res.data.map(item => {
            return JSON.parse(item)
        })
        new result(ctx, 'SUCCESS', 200, data).answer()
    } catch (error) {
        new result(ctx, "服务器出现错误", 500).answer()
        return error
    }
})

// 查询菜品分类
router.get('/foodtype', new showtoken().m, async ctx => {
    const query = `db.collection('food-type').get()`
    try {
        const res = await new getToken().postlive(selurl, query)
        const data = res.data.map(item => {
            return JSON.parse(item)
        })
        new result(ctx, 'SUCCESS', 200, data).answer()
    } catch (error) {
        new result(ctx, "服务器出现错误", 500).answer()
        return error
    }
})

// 查询菜单列表
router.get("/shoplist",  async ctx => {
    const query = `db.collection('shoplist').get()`
    try {
        const res = await new getToken().postlive(selurl, query)
        const data = res.data.map(item => {
            return JSON.parse(item)
        })
        new result(ctx, 'SUCCESS', 200, data).answer()
    } catch (error) {
        new result(ctx, "服务器出现错误", 500).answer()
        return error
    }
})

// 分页接口
router.get('/sppaging', new showtoken().m, async ctx => {
    const {
        page
    } = ctx.query
    const sk = page * 10
    const query = `db.collection('shoplist').orderBy('cretime','desc').limit(10).skip(${sk}).get()`
    try {
        const res = await new getToken().postlive(selurl, query)
        const data = res.data.map(item => {
            return JSON.parse(item)
        })
        const tatal = {
            total: res.pager.Total
        }
        const arry = {
            ...{
                result: data
            },
            ...tatal
        }
        new result(ctx, "成功", 200, arry).answer()
    } catch (error) {
        new result(ctx, "服务器出现错误", 500).answer()
        return
    }
})

//获取商家信息

router.get('/showsetSeller', new showtoken().m, async ctx => {
    const query = `db.collection("setSeller").get()`
    try {
        let res = await new getToken().postlive(selurl, query)
        let data = res.data.map(item => {
            return JSON.parse(item)
        })
        new result(ctx, '成功', 200, data).answer()
    } catch (error) {
        new result(ctx, '服务器出现错误', 500).answer()
    }
})

// 下架商品
router.post('/deshoplist', new showtoken().m, async ctx => {
    let data = ctx.request.body
    let dauid = data._id
    let dation = JSON.stringify(data.classification)
    const query = `db.collection('shoplist').doc('${dauid}').update({data:{operation:false}})`
    const fonum = `db.collection('food-type').where({label:'${dation}'}).update({data:{fonum:db.command.inc(1)}})`
    try {
        await new getToken().postlive(updurl, query)
        await new getToken().postlive(updurl, fonum)
        new result(ctx, '下架成功').answer()
    } catch (error) {
        new result(ctx, '服务器出错', 500).answer()
    }
})



module.exports = router